<?php
namespace app\Common\model;
use app\common\model\BaseModel;
/**
 * 管理员模型
 * Class AdminModel
 * @package app\Common\model
 */
class ArticleModel extends BaseModel
{
    protected $name = 'article';

    /**
     * 查询列表
     * @param array $where 条件
     * @param bool $type 类型:true分页,false查询所有
     */
    public static function list($where = [], $page, $list_rows = 10, $type = true)
    {
        if($type){

           return self::alias('a')
                ->join('t_article_category ac', 'a.cate_id=ac.id')
                ->field('a.*, ac.name as cate_name')
                ->paginate([
                    'list_rows'=> $list_rows,
                    'page' => $page,
                ]);
        } else {
            return self::where($where)->select();
        }
    }

    /**
     * 查询单条记录
     * @param array $where 条件
     */
    public  static function findById($where = [])
    {
        return self::where($where)->find();
    }

    /**
     * 添加文章
     * @param $data 数据
     * @return ArticleModel|\think\Model
     */
    public static function addArticle($data)
    {
        return self::create($data);
    }

    /**
     * 编辑文章
     * @param $where 条件
     * @param $data 数据
     * @return ArticleModel
     */
    public static function saveArticle($where, $data)
    {
        return self::where($where)->update($data);
    }

    /**
     * 删除文章
     * @param array $where 条件
     * @return bool
     */
    public static  function del($where = [])
    {
        if(!$where){
            return  fasle;
        }
        return self::where($where)->delete();
    }

}
